<?php

namespace App\Modules\Admin\Services;

use App\Modules\Admin\Entities\Article\Article;

class ArticleService extends BaseService
{
    public function __construct(Article $article)
    {
        $this->model = $article;
        $this->with = ['category'];
    }

    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $search = trim($search);
                $query->where('article_title', 'LIKE', '%' . $search . '%')
                    ->whereOr('article_keywords', 'LIKE', $search . '%')
                    ->whereOr('article_description', 'LIKE', $search . '%');
            }
            // 文章分类
            $category_id = $request->input('category_id', -1);
            if ($category_id > -1){
                $query->where('category_id', '=', $category_id);
            }
            // 置顶
            $set_top = $request->input('set_top', -1);
            if ($set_top > -1){
                $query->where('set_top', '=', $set_top);
            }
            // 推荐
            $is_recommend = $request->input('is_recommend', -1);
            if ($is_recommend > -1){
                $query->where('is_recommend', '=', $is_recommend);
            }
            // 是否公开
            $is_public = $request->input('is_public', -1);
            if ($is_public > -1){
                $query->where('is_public', '=', $is_public);
            }
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }
}
